package com.rabbiter.pm.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.rabbiter.pm.mapper.SignMapper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class ScheduledTasks {

    private static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class);

    @Resource
    private SignMapper signMapper;

    @Scheduled(cron = "0 0 0 * * ?")
    public void clearDataByDesgin(){
        //日志
        logger.info("---------您好，定时任务开始执行！---------"+new SimpleDateFormat("HH:mm:ss").format(new Date()));
        //调用删除数据库数据方法
        clearData();
        logger.info("---------真不错，定时任务执行成功！---------"+new  SimpleDateFormat("HH:mm:ss").format(new Date()));
    }

    //删除数据库数据方法
    private void clearData() {
        try {
            //调用service层的方法 删除数据库数据
            signMapper.delete(null);
        } catch (Exception e) {
            logger.error("清理数据失败，失败原因：" + e.getMessage());
        }
    }
}
